package com.sunnada.edu.system.pub.filter;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.filter.OncePerRequestFilter;

import com.sunnada.edu.system.login.service.LoginMethods;
import com.sunnada.edu.system.pub.common.AppContext;
import com.sunnada.edu.system.pub.common.Constants;
import com.sunnada.edu.system.pub.common.DateInfo;
import com.sunnada.edu.system.pub.dao.PubDao;
import com.sunnada.edu.system.pub.listener.SessionListener;
import com.sunnada.edu.system.pub.util.Utils;
import com.sunnada.edu.system.pub.xml.AppIniXml;

/**
 * explain过滤类
 * 
 * @author chs 2014-10-16 version 1.0
 */
public class ParameterFilter extends OncePerRequestFilter {

	/*
	 * 数据过滤
	 * 
	 * @see
	 * org.springframework.web.filter.OncePerRequestFilter#doFilterInternal(
	 * javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain)
	 */
	protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws ServletException,
			IOException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		HttpSession session = request.getSession();
		String webloginurl = request.getContextPath() + "loginController/error500.shtml";

		// String url = request.getRequestURI();
		String ip = Utils.getIp(request);

		boolean flag = true;

		/*********************** 区域过滤开始 **********************************************************/
		String areaCode = request.getParameter("areaCode");
		String loginUserAreaCode = (String) session.getAttribute(Constants.USER_AREA_CODE);
		if (Utils.isNotNull(areaCode) && Utils.isNotNull(loginUserAreaCode)) {
			if (!areaCode.startsWith(loginUserAreaCode)) {
				flag = false;
			}
		}
		/*********************** 区域过滤结束 **********************************************************/

		if (flag == false) {
			gotoPage(request, response, webloginurl);
		} else {
			chain.doFilter(request, response);
		}
	}

	/**
	 * 跳转至指定界面
	 * 
	 * @param request
	 * @param response
	 * @param weburl
	 * @throws IOException
	 */
	private void gotoPage(HttpServletRequest request, HttpServletResponse response, String weburl) throws IOException {
		if ("XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))) {
			PrintWriter printWriter = null;
			try {
				response.setContentType("text/html;charset=utf-8");
				response.setCharacterEncoding("utf-8");
				response.setStatus(998);
				printWriter = response.getWriter();
				printWriter.print(weburl);
			} finally {
				if (printWriter != null) {
					printWriter.close();
				}
			}
		} else {
			response.sendRedirect(weburl);
		}
	}

}
